#include "recentcontactevent.h"
#include "appchat/accountmanager.h"
#include "account/accountbuddy.h"
#include "account/accountuser.h"


RecentContactEvent::Item::Item(qint64 friendId,
                               qint64 lastContactTime,
                               qint64 chid,
                               QString message,
                               QString name,
                               int type) :
    m_friendId(friendId),
    m_lastContactTime(lastContactTime),
    m_chid(chid),
    m_message(message),
    m_name(name),
    m_type(type)
{
}


RecentContactEvent::RecentContactEvent(const QVariantList &list, QObject *parent) :
    AbstractEvent(parent)
{
    parseList(list);
}


RecentContactEvent::~RecentContactEvent()
{
    qDeleteAll(m_itemList);
}


void RecentContactEvent::parseList(const QVariantList &list)
{
    int32_t userId = AccountManager::instance()->getUserId();

    bool ok;
    foreach (QVariant item, list) {
        if (item.type() != QVariant::Map)
            return;

        QVariantMap map = item.toMap();

        // Parse type
        if (!map.contains("type"))
            return;

        QVariant typeVariant = map.value("type");
        int type = typeVariant.toString().toInt(&ok);
        if (!ok)
            continue;
        if (type != 1)
            continue; // For only private chat

        // Parse fid
        if (!map.contains("fids"))
            return;

        QVariant friendIdVariant = map.value("fids");
        QVariantList friendIdList = friendIdVariant.toList();
        if (friendIdList.size() != 2)
            continue; // Private chat have just two ids

        qint64 firstId = friendIdList.at(0).toString().toLongLong(&ok);
        if (!ok)
            continue;
        qint64 friendId = 0;
        if (firstId != userId) {
            friendId = firstId;
        } else {
            qint64 secondId = friendIdList.at(1).toString().toLongLong();
            if (secondId != userId) {
                friendId = secondId;
            } else {
                continue;
            }
        }

        // Parse chid
        if (!map.contains("chid"))
            return;

        QVariant chidVariant = map.value("chid");
        qint64 chid = chidVariant.toString().toLongLong(&ok);
        if (!ok)
            continue;

        // Parse ltime
        if (!map.contains("ltime"))
            return;

        QVariant lastContactTimeVariant = map.value("ltime");
        qint64 lastContactTime = lastContactTimeVariant.toString().toLongLong(&ok);
        if (!ok)
            continue;

        // Parse message
        if (!map.contains("mess"))
            return;

        QVariant messageVariant = map.value("mess");
        QString message = messageVariant.toString();

        // parse name
        if (!map.contains("name"))
            return;

        QVariant nameVariant = map.value("name");
        QString name = nameVariant.toString();

        m_itemList.append(new Item(friendId,
                                lastContactTime,
                                chid,
                                message,
                                name,
                                type));
    }
}


void RecentContactEvent::process()
{
    AccountManager::instance()->updateRecentContactTime(*this);
}


AbstractEvent::EventType RecentContactEvent::eventType() const
{
    return AbstractEvent::RecentContactEvent;
}
